/**
 * 增加角色js
 * Created by zhangsi on 2015/10/16.
 */
var roleAdd = new XFApp.client();
XFApp.client.prototype.add = {
    isSubmitting : false,
    successCode : 10000,
    menuTree : null,
    urlItems : {
        menuListUrl : basePath + "/menu/ajax/list",
        saveURL : basePath +"/role/ajax/saveOrUpdate",
        roleListUrl : basePath + "/role"
    },
    loadMenus : function(){
        var _this = this;
        $.ajax({
            url:this.urlItems.menuListUrl,
            dataType:'json',
            type:'post',
            success:function(data){
                if(data.code != roleAdd.add.successCode){
                    webDialogUtil.simpleAlert(data.msg);
                }else{
                    var menus = $("#role_menus").val();
                    var checkedIds = {};
                    if(menus != null && menus != 'undefined' && menus != ''){
                        menus = menus.split(',');
                        for(var i=0; i<menus.length; i++){
                            var menuId = menus[i];
                            //0为初始状态，1为选中，2为未选中
                            checkedIds[menuId] = 0;
                        }
                    }
                    var $expandibleTree = $('#treeview-expandible').treeview({
                        data: data.result,
                        levels : 3,
                        nodesName : 'subMenus',
                        text : 'description',
                        showCheckbox: true,
                        nodeIdName : 'id',
                        checkedIds : checkedIds,
                        onNodeCollapsed: function(event, node) {
                            $('#expandible-output').prepend('<p>' + node.text + ' was collapsed</p>');
                        },
                        onNodeExpanded: function (event, node) {
                            $('#expandible-output').prepend('<p>' + node.text + ' was expanded</p>');
                        }
                    });
                    _this.menuTree = $expandibleTree;
                }
            },
            error:function(){
                webDialogUtil.simpleAlert("出现异常,请联系开发人员");
            }
        });
    },
    saveOrUpdate : function(){
        var _this = this;
        //把选中的id设置为一个数组
        var menuIds = "";
        $("#treeview-expandible li.node-checked").each(function(obj){
            var _li = $(obj);
        });
        var checkIds = _this.menuTree.treeview("getCheckedIds");
        for(idKey in checkIds){
            if(checkIds[idKey] == 1){
                menuIds += idKey + ",";
            }
        }
        if(menuIds.length > 0){
            menuIds = menuIds.substr(0,menuIds.length - 1);
        }
        $("#role_menus").val(menuIds);
        $.ajax( {
            url:this.urlItems.saveURL,
            data:$('#edit_form').serialize(),
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(data.code == _this.successCode){
                    webDialogUtil.callbackConfrimAlert(data.msg, function(){window.location = roleAdd.add.urlItems.roleListUrl});
                }else{
                    webDialogUtil.simple(data.msg);
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    formValidate : function(){
        $('#editForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                enterpriseCode: {
                    group: '.col-lg-4',
                    validators: {
                        notEmpty: {message: '客户编码不能为空'}
                    }
                },
                consingeeTel: {
                    group: '.col-lg-4',
                    validators: {
                        notEmpty: {message: '请填写收货人电话'}
                    }
                }
            }
        });
    },
    event : function(){
        var _this = this;
        _this.loadMenus();
        $("#save_role").on("click",function(){
            $('#edit_form').bootstrapValidator('validate');
            if($('#edit_form').data('bootstrapValidator').isValid()){
                _this.saveOrUpdate();
            }
        });
        //表单验证
        _this.formValidate();
    }
}
$(function(){
    roleAdd.add.event();
});
